package com.cskaoyan.tool;

import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

/**
 * xdx
 * 2020/12/11
 * 9:09
 * 测试Url是不是可以访问
 */
public class TestUrl {
    public static void testUrl(String urlString){

        long lo = System.currentTimeMillis();
        URL url;
        try {
            url = new URL(urlString);
            InputStream in = url.openStream();
            System.out.println("连接可用");
        } catch (Exception e1) {
            System.out.println("连接打不开!");
            url = null;
        }

        System.out.println(System.currentTimeMillis()-lo);
    }

    public static boolean testUrlWithTimeOut(String urlString,int timeOutMillSeconds){
        long lo = System.currentTimeMillis();
        URL url;
        try {
            url = new URL(urlString);
            URLConnection co =  url.openConnection();
            co.setConnectTimeout(timeOutMillSeconds);
            co.connect();
            System.out.println("连接可用");
            return true;
        } catch (Exception e1) {
            System.out.println("连接打不开!");
            url = null;
            return false;
        }finally {
            System.out.println(System.currentTimeMillis()-lo);
        }

    }
}
